package oocis.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class SVNClient {
	
	public SVNClient(){
		
	}
	
	public static int checkout(String _url, String _localDir) throws IOException{
		System.out.println("bin\\svn checkout " + _url + " " + _localDir);
		ExtCall c = new ExtCall("bin\\svn checkout " + _url + " " + _localDir);
		return c.exec();
	}
	
	public static int update(String _url, String _localDir) throws IOException{
		System.out.println("bin\\svn update " + _url + " " + _localDir);
		ExtCall c = new ExtCall("bin\\svn update " + _url + " " + _localDir);
		return c.exec();
	}
	
	public static int add(String _localDir, String _filename) throws IOException{
		System.out.println("bin\\svn add " + _localDir + _filename);
		ExtCall c = new ExtCall("bin\\svn add " + _localDir + _filename);
//		ExtCall c = new ExtCall("bin\\svn");
		return c.exec();
	}
	
	public static int remove(String _url, String _filename, String _message) throws IOException{
		System.out.println("bin\\svn remove " + _url + _filename + " -m " + _message);
		ExtCall c1 = new ExtCall("bin\\svn remove " + _url + _filename + " -m " + _message);
		return c1.exec();
	}
	
	public static int commit(String _url, String _localFile, String _message) throws IOException{
		System.out.println("bin\\svn commit " + _localFile + " -m " + _message);
		ExtCall c2 = new ExtCall("bin\\svn commit " + _localFile + " -m " + _message);
		return c2.exec();
	}
	
	public static int export(String _url, String _remoteDir, String _remoteFilename, String _localFile) throws IOException{
		System.out.println("bin\\svn export " + _url + _remoteDir + _remoteFilename + " " + _localFile);
		ExtCall c = new ExtCall("bin\\svn export " + _url + _remoteDir + _remoteFilename + " " + _localFile);
		return c.exec();
	}
	
	public static int search(String _url, String _remoteDir, String _remoteFilename) throws IOException{
		System.out.println("bin\\svn list " + _url + _remoteDir + _remoteFilename);
		ExtCall c = new ExtCall("bin\\svn list " + _url + _remoteDir + _remoteFilename);
		return c.exec();
	}
	public static ArrayList <String> list(String _url, String _remoteDir) throws IOException{
		ArrayList<String> _list = new ArrayList<String>();
		java.lang.Process p = Runtime.getRuntime().exec("cmd /c bin\\svn list " + _url + _remoteDir);
		
		InputStreamReader isr = new InputStreamReader(p.getInputStream());
		BufferedReader br = new BufferedReader(isr);
		
		String l;
		
		while ((l = br.readLine()) != null){
			_list.add(l);
		}
		
		return _list;

	}

}
